//
//  YLProductBuyingCycleCell.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/29.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLProductBuyingCycleCell.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
#import "YLProductModel.h"

@interface YLProductBuyingCycleCell ()
@property (weak, nonatomic) IBOutlet UIView *cycleBaseView;
@property (nonatomic, strong) NSMutableArray *labels;
@end


@implementation YLProductBuyingCycleCell

- (NSMutableArray *)labels
{
    if (!_labels) {
        _labels = [NSMutableArray arrayWithCapacity:4];
    }
    return _labels;
}

- (void)setMonth:(NSInteger)month
{
    _month = month;
    
    
    
    NSCalendar *calender2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    [calender2 setFirstWeekday:2];// 国外是从周日 开始算的 我们是周一 所以 写了2
    
    //NSDateComponents *components2 = nil;
    
    NSDate *currentDate = [NSDate date];
    
    
   // NSTimeZone * zone = [NSTimeZone systemTimeZone];
    
   // NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate * nowDate = currentDate;
    //[currentDate dateByAddingTimeInterval:interval];
    
    // components2 = [calender2 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
    
    NSDateComponents *adcomps = [[NSDateComponents alloc]init];
    
    [adcomps setYear:0];
    
    [adcomps setMonth:+month];
    
    [adcomps setDay:-1];
    
    NSDate *newdate = [calender2 dateByAddingComponents:adcomps toDate:nowDate options:0];
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *nowDateStr = [dateFormatter stringFromDate:nowDate];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *newDateStr = [dateFormatter stringFromDate:newdate];
    
    NSArray *dates = @[nowDateStr, nowDateStr, newDateStr, newDateStr];
    
    if (dates.count == self.labels.count) {
        for (NSInteger i = 0; i < self.labels.count; i ++) {
            UILabel *label = self.labels[i];
            label.text = dates[i];
        }
    }
    
    
    

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    UIView *line = [[UIView alloc] init];
    [self.cycleBaseView addSubview:line];
    line.backgroundColor = YLMainColor;
    __weak typeof(self) weakSelf = self;
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.cycleBaseView);
        make.height.mas_equalTo(2);
        make.left.mas_equalTo(weakSelf.cycleBaseView).mas_offset(20);
        make.right.mas_equalTo(weakSelf.cycleBaseView).mas_equalTo(-20);
    }];
    
    NSArray <NSString *> *titles = @[@"申购日", @"收益起始日", @"收益到期日", @"预期收款日"];

    for (NSInteger i = 0 ; i < 4 ; i ++) {
        UIView *cycie = [[UIView alloc] init];
        cycie.backgroundColor = YLMainColor;
        [self addSubview:cycie];
        
        [cycie mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(line);
            make.left.mas_equalTo((YLScreenW - 40) / 3.0 * i + 20);
            make.height.width.mas_equalTo(6);
        }];
        cycie.layer.masksToBounds = YES;
        cycie.layer.cornerRadius = 3.0;
        
        UILabel *label = [[UILabel alloc] init];
        label.font = [UIFont systemFontOfSize:13.0f];
        label.textColor = YLColor(170, 170, 170);
        label.text = titles[i];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.mas_equalTo(weakSelf).mas_offset(10);
            } else if (i == 3) {
                make.right.mas_equalTo(weakSelf).mas_offset(-5);
            } else {
                make.centerX.mas_equalTo(cycie);
            }
                make.bottom.mas_equalTo(cycie.mas_top).mas_offset(-6);
        }];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.labels addObject:titleLabel];
        titleLabel.font = [UIFont systemFontOfSize:13.0f];
        titleLabel.textColor = YLColor(80, 80, 80);
        [_cycleBaseView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.mas_equalTo(label);
            } else if (i == 3) {
                make.right.mas_equalTo(label);
            } else {
                make.centerX.mas_equalTo(label);
            }
            make.top.mas_equalTo(cycie.mas_bottom).mas_offset(6);
        }];
        
    }
}

+ (NSString *)cellID
{
    return @"YLProductBuyingCycleCell";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
